package xpn.business.fund_return.entity.return_application_form;

import org.springframework.context.ApplicationContextException;
import org.springframework.stereotype.Service;
import xpn.business.fund_return.entity.frcase.FrCase;
import xpn.business.fund_return.entity.frcase.FrCaseDao;
import xpn.business.fund_return.entity.frcase.valueobject.CaseStatusCommon;
import xpn.business.fund_return.entity.frcase.valueobject.CaseWriteStatus;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class ReturnApplicationFormService {
    @Resource
    private ReturnApplicationFormDao returnApplicationFormDao;

    @Resource
    private FrCaseDao frCaseDao;

//    @Resource
//    private FrCaseService frCaseService;

    public ReturnApplicationForm save(ReturnApplicationForm returnApplicationForm) {
        if(Objects.isNull(returnApplicationForm.getFilingNo())) {
            throw new ApplicationContextException("请输入案件编号");
        }
        if(Objects.isNull(returnApplicationForm.getDefraudedAmount())) {
            throw new ApplicationContextException("请输入被骗金额");
        }
        if (Objects.isNull(returnApplicationForm.getId())) {
            if (Objects.nonNull(returnApplicationForm.getCaseId())) {
                ReturnApplicationForm exist = returnApplicationFormDao.findAllByCaseId(returnApplicationForm.getCaseId());
                if (Objects.nonNull(exist)) {
                    throw new ApplicationContextException("该表单已存在，请勿重复创建");
                }
            } else {
                FrCase frCase = new FrCase();
                //同步表单数据
                frCase.setCaseCodeCommon(returnApplicationForm.getFilingNo());
                frCase.setCaseNameCommon(returnApplicationForm.getName()+"被骗"+returnApplicationForm.getDefraudedAmount()+"元案件");
                frCase.setVictimName(returnApplicationForm.getVictimName());
                frCase.setCaseHandlingUnit(returnApplicationForm.getCaseHandlingUnit());
                frCase.setCaseHandlerA(returnApplicationForm.getCaseHandlerA());
                frCase.setCaseHandlerB(returnApplicationForm.getCaseHandlerB());
                frCase.setBlockingBankAccounts(returnApplicationForm.getBlockingBankAccounts());
                frCase.setReturnAccount(returnApplicationForm.getReturnAccount());
                frCase.setCaseWriteStatus(CaseWriteStatus.Form);
                frCase.setCaseStateCommon(CaseStatusCommon.Writing);
                frCase = frCaseDao.save(frCase);
                returnApplicationForm.setCaseId(frCase.getId());
            }
        } else {
            FrCase frCase = frCaseDao.getOne(returnApplicationForm.getCaseId());
            frCase.setCaseCodeCommon(returnApplicationForm.getFilingNo());
            if(Objects.isNull(frCase.getCaseNameCommon()) && frCase.getCaseNameCommon().equals("")) {
                frCase.setCaseNameCommon(returnApplicationForm.getName()+"被骗"+returnApplicationForm.getDefraudedAmount()+"元案件");
            }
            frCase.setVictimName(returnApplicationForm.getVictimName());
            frCase.setCaseHandlingUnit(returnApplicationForm.getCaseHandlingUnit());
            frCase.setCaseHandlerA(returnApplicationForm.getCaseHandlerA());
            frCase.setCaseHandlerB(returnApplicationForm.getCaseHandlerB());
            frCase.setBlockingBankAccounts(returnApplicationForm.getBlockingBankAccounts());
            frCase.setReturnAccount(returnApplicationForm.getReturnAccount());
            frCaseDao.save(frCase);
        }
//        frCaseService.changeWriteStatus(returnApplicationForm.getCaseId(), CaseWriteStatus.Form);
        return returnApplicationFormDao.save(returnApplicationForm);
    }
}
